// pages/robOrder/robOrder.js
const api = require('../../utils/http.js')
const app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		typeId: 0,
		page: 1,
		isEmpty: null,
		navArr1: ['全部','未出库', '已出库', '待送达', '待确认', '已完成'],
		orderList: [],
		total: '',
		pageTotal: 10,
		isTop: false,
		scrollLeft: 0
	},
	toOrderDetails(e) {
		let orderInfo = e.currentTarget.dataset.orderinfo
		wx.navigateTo({
			url: '/pages/takeOrdersDetail/takeOrdersDetail1?orderId=' + orderInfo.id + '&total=' + orderInfo.jine
		})
	},
	backTop() {
		wx.pageScrollTo({
			scrollTop: 0,
			duration: 300
		})
	},
	changeType(e) {
		let typeId = e.currentTarget.id
		if (typeId >= 2) {
			this.setData({
				scrollLeft: 40
			})
		} else {
			this.setData({
				scrollLeft: 0
			})
		}
		console.log(typeId)
		this.data.page = 1
		this.setData({
			typeId
		})
		this.http()
	},
	http() {
		wx.showLoading({
			title: '加载中',
		})
		console.log(this.data.typeId)
		console.log(this.data.page)
		let typeId
		switch (parseInt(this.data.typeId)) {
      case 0:
        typeId = '全部';
        break;
			case 1:
				typeId = '未出库';
				break;
			case 2:
				typeId = '已出库';
				break;
			case 3:
				typeId = '待送达';
				break;
			case 4:
				typeId = '待确认';
				break;
			case 5:
				typeId = '已完成';
				break;
			// default:
			// 	typeId = 0;
			// 	break;
		}
		let uid = app.globalData.uid;
		api.fetchApi('procedureList', {
			p: 'R1001004',
			k: this.data.page,
			j: this.data.pageTotal,
			cs: `${uid}|${typeId}`,
		}).then(res => {
			if (res.data.code == 1) {
				let orderList = this.data.orderList
				if (this.data.page == 1) {
					orderList = res.data.results //如果是想下滑动，刷新数据 就让items等于最新数据
				} else {
					orderList = orderList.concat(res.data.results) //否则就把数据拼接
				}
				let total = res.data.results.length
				this.setData({
					orderList,
					total
				})
			} else {
				this.data.page--;
				wx.showToast({
					title: `${res.data.errorMsg}`,
					icon: 'none'
				})
			}
			wx.hideLoading();
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log(options)
		let typeId = options.id || 0
		let page = 1
		this.setData({
			typeId,
			page,
		})
		this.http()
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {
		this.data.page = 1
		setTimeout(() => {
			wx.stopPullDownRefresh()
		}, 1500)
		this.http()
	},
	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		console.log(this.data.total)
		console.log(this.data.pageTotal, 111)
		this.data.page++;
		if (this.data.total < this.data.pageTotal) {
			wx.showToast({
				title: `没有更多数据`,
				icon: 'none'
			})
		} else {
			wx.showLoading({
				title: '玩命加载中',
			})
			this.http()
		}
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	/**
	 * 获取滚动条当前位置
	 */
	onPageScroll: function(e) {
		if (e.scrollTop > 500) {
			this.setData({
				isTop: true
			})
		} else {
			this.setData({
				isTop: false
			})
		}
	},
})
